package com.changgou.goods.service.myimpl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;

import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service("brandServiceImpl_test")
public class BrandServiceImpl_test implements BrandService {


    @Autowired
    private BrandMapper brandMapper;


    @Override
    public List<Brand> findAll() {
        List<Brand> brands = brandMapper.selectAll();
        return brands;
    }

    @Override
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;

    }

    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);

    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = getExample(searchMap);
        List<Brand> brands = brandMapper.selectByExample(example);
        return brands;
    }


    public Example getExample(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        if (searchMap != null) {
            if (!StringUtils.isEmpty(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            if (!StringUtils.isEmpty(searchMap.get("letter"))) {
                criteria.andEqualTo("letter", searchMap.get("letter"));
            }
        }
        // 为example添加criteria查询条件，关系为与
        example.and(criteria);
        return example;
    }


    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Brand> page1 = (Page<Brand>) brandMapper.selectAll();
        return page1;
    }


    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);

        Example example = getExample(searchMap);

        Page<Brand> page1 = (Page<Brand>) brandMapper.selectByExample(example);
        return page1;
    }

    @Override
    public List<Map> findBrandListByCategoryName(String categoryName) {
        List<Map> list = brandMapper.findBrandListByCategoryName(categoryName);
        return list;
    }


}
